/**
 * Sprite.
 *
 * Released under Apache License, Version 2.0.
 */

package bsp.android.spritefwk;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class Sprite
{
	protected String mId;
	protected boolean bSelected;
	protected ImageHolder mStandardImage, mSelectedImage;
	protected int mPosX, mPosY, mWidth, mHeight, mOffsetX, mOffsetY;
	protected SpriteSelectionChangeListener mListener;
	
	/**
	 * Constructor to create a sprite from the image holder
	 * 
	 * @param id Sprite ID.
	 * @param listener Reference to callback that will handle sprite selection changes.
	 * @param standardImage Reference to standard image.
	 * @param selectedImage Reference to selected image.
	 * @param posX X coordinate of starting location of sprite on the screen.
	 * @param posY Y coordinate of starting location of sprite on the screen.
	 * @param width Width of the sprite.
	 * @param width Height of the sprite.
	 * @param offsetX X coordinate of starting location of the sprite in the standard and selected images.
	 * @param offsetY Y coordinate of starting locaiton of sprite in the standard and selected images.
	 */
	public Sprite(String id, SpriteSelectionChangeListener listener, ImageHolder standardImage, ImageHolder selectedImage, int posX, int posY, int width, int height, int offsetX, int offsetY)
	{
		mId = id;
		mListener = listener;
		mStandardImage = standardImage;
		mSelectedImage = selectedImage;
		bSelected = false;
		
		// position of the image, its width and height
		mPosX = posX;
		mPosY = posY;
		mWidth = width;
		mHeight = height;
		
		// offset coordinates where this sprite starts in the image holder
		mOffsetX = offsetX;
		mOffsetY = offsetY;
	}
	
	public String getId()
	{
		return mId;
	}
	
	public void setX(int val)
	{
		mPosX = val;
	}
	
	public void setY(int val)
	{
		mPosY = val;
	}
	
	public void setWidth(int val)
	{
		mWidth = val;
	}
	
	public void setHeight(int val)
	{
		mHeight = val;
	}
	
	public void setOffsetX(int val)
	{
		mOffsetX = val;
	}
	
	public void setOffsetY(int val)
	{
		mOffsetY = val;
	}
	
	public Rect getRect()
	{
		Rect r = new Rect(mPosY, mPosX, mPosY+mHeight, mPosX+mWidth);
		return r;
	}
	
	public Rect getOffsetRect()
	{
		Rect r = new Rect(mOffsetY, mOffsetX, mOffsetY+mHeight, mOffsetX+mWidth);
		return r;
	}
	
	/**
	 * Set a listener for the sprite.
	 */
	public void setListener(SpriteSelectionChangeListener listener)
	{
		mListener = listener;
	}
	
	/**
	 * Get a reference to the listener set for the sprite.
	 */
	public SpriteSelectionChangeListener getListener()
	{
		return mListener;
	}
	
	/**
	 * Check if a listener is present for the sprite.
	 */
	public boolean hasListener()
	{
		if ( mListener != null ) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * Check if the sprite is selected.
	 */
	public boolean isSelected()
	{
		return bSelected;
	}
	
	/**
	 * When a sprite's "selected" flag is changed, a message
	 * will be sent to the listener of the sprite, if a listener
	 * for the sprite is registered.
	 */
	public void setSelected(boolean flag)
	{
		bSelected = flag;
		if ( mListener != null ) {
			mListener.selectionChanged(mId, bSelected);
		}
	}
	
	/**
	 * Toggle the sprite's selected status flag.
	 */
	public void toggleSelection()
	{
		bSelected = !bSelected;
		if ( mListener != null ) {
			mListener.selectionChanged(mId, bSelected);
		}
	}
	
	/**
	 * Set position of the sprite at given location.
	 */
	public void setPosition(int x, int y)
	{
		mPosX = x;
		mPosY = y;
	}
	
	/**
	 * Set position of the sprite, with given location as the center.
	 */
	public void setPositionCentered(int x, int y)
	{
		mPosX = x - (mWidth / 2);
		mPosY = y - (mHeight / 2);
	}
	
	public int getPositionX()
	{
		return mOffsetX;
	}
	
	public int getPositionY()
	{
		return mOffsetY;
	}
	
	public int getWidth()
	{
		return mWidth;
	}
	
	public int getHeight()
	{
		return mHeight;
	}
	
	/**
	 * Check if given point is inside the sprite.
	 */
	public boolean isInside(int x, int y)
	{
		boolean retVal = false;
		
		if ( x >= mOffsetX && x <= (mOffsetX + mWidth) ) {
			if ( y >= mOffsetY && y <= (mOffsetY + mHeight) ) {
				retVal = true;
			}
		}
		
		return retVal;
	}
	
	/**
	 * Draw the sprite.
	 */
	public void draw(Canvas c, Paint p)
	{
		Rect srcRect, destRect;
		
		srcRect = new Rect(mPosX, mPosY, mPosX + mWidth, mPosY + mHeight);		
		destRect = new Rect(mOffsetX, mOffsetY, mOffsetX + mWidth, mOffsetY + mHeight);
		if ( true == bSelected ) {
			if ( mSelectedImage != null ) {
				c.drawBitmap(mSelectedImage.getGraphic(), srcRect, destRect, p);
			}
		} else {
			if ( mStandardImage != null ) {
				c.drawBitmap(mStandardImage.getGraphic(), srcRect, destRect, p);
			}
		}
	}
	
	/**
	 * Draw the sprite.
	 */
	public void draw(Canvas c)
	{
		draw(c, null);
	}
	
	public void cleanup()
	{
		mStandardImage = null;
		mSelectedImage = null;
		mListener = null;
	}
}

/* end */
